//
//  JokesCellTwo.m
//  Quick_News
//
//  Created by silently on 15-1-29.
//  Copyright (c) 2015年 zkj. All rights reserved.
//

#import "JokesCellTwo.h"
#import "PerformanceViewController.h"


@implementation JokesCellTwo
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 初始化子视图属性, 不要给子视图指定frame
        
        self.myView = [[UIView alloc] init];
        
        [self.myView.layer setBorderColor:[UIColor grayColor].CGColor];
//        self.myView.backgroundColor = [UIColor colorWithRed:207/255.0 green:223/255.0 blue:229/255.0 alpha:0.8];
        [self.myView.layer setCornerRadius:7.f];
        [self.myView.layer setBorderWidth:0.5f];
        
        [self.contentView addSubview:self.myView];
        [self.myView release];
        
        
        self.digestLabel = [[UILabel alloc] init];
        [self.myView addSubview:self.digestLabel];
        self.digestLabel.numberOfLines = 0;
        [self.digestLabel release];
        
        self.lineLabel = [[UILabel alloc] init];
        self.lineLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"line.png"]];
        [self.myView addSubview:self.lineLabel];
        [self.lineLabel release];
        
        
        
        self.collectButton = [UIButton buttonWithType:UIButtonTypeSystem];
        
//        [self.collectButton setTitle:@"collect" forState:UIControlStateNormal];
        [self.collectButton addTarget:self action:@selector(collectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.myView addSubview:self.collectButton];
        
        self.goodButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.goodButton addTarget:self action:@selector(goodButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.myView addSubview:self.goodButton];
        
        self.goodLabel = [[UILabel alloc] init];
        [self.myView addSubview:self.goodLabel];
        [self.goodLabel release];
        
    }
    return self;
}

// 指定子视图frame.并赋值
- (void)layoutSubviews
{

    [super layoutSubviews];
    self.myView.frame = self.contentView.bounds;
    [self.myView sizeToFit];
    self.myView.frame = CGRectMake(10, 10, self.contentView.frame.size.width - 20, self.contentView.frame.size.height - 20);
    

    self.lineLabel.frame = CGRectMake(0, self.myView.frame.size.height - 45, self.myView.frame.size.width, 10);

    
    self.collectButton.frame = CGRectMake(self.myView.frame.size.width - 70, self.myView.frame.size.height - 35, 30, 30);

    
    
    
    self.goodButton.frame = CGRectMake(40, self.myView.frame.size.height - 35, 30, 30);

    
    self.goodLabel.frame = CGRectMake(70, self.myView.frame.size.height - 35, 100, 30);
    
    [self addViews];
    

}

- (void)addViews
{
    self.digestLabel.frame = self.myView.bounds;
    [self.digestLabel sizeToFit];
    self.digestLabel.frame = CGRectMake(10, 0, self.myView.frame.size.width - 20, self.myView.frame.size.height - 40);
    self.cellJokes.digest = [self.cellJokes.digest stringByReplacingOccurrencesOfString:@"（动态图可点击进入查看哦~！）." withString:@""];
    self.cellJokes.digest = [self.cellJokes.digest stringByReplacingOccurrencesOfString:@" &quot;" withString:@""];
    [self setValues];
}
- (void)setValues
{
    self.digestLabel.text = self.cellJokes.digest;
    self.digestLabel.font = [UIFont systemFontOfSize:16];
    
    self.collectButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"collectEmpty.png"]];
    
    self.goodButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"goodEmpty.png"]];
    
    self.goodLabel.text = [NSString stringWithFormat:@":%@", self.cellJokes.upTimes];

}

// 点击收藏方法
- (void)collectButtonAction:(UIButton *)button
{
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance]; 
    
    if (self.flag) {
        self.collectButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"collectEmpty.png"]];
        [dbHandler removeJokes:self.cellJokes];
    } else {
        
        self.collectButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"collectFull.png"]];
        [dbHandler insertJokes:self.cellJokes];
    }
    NSLog(@"查找数据:%@", [dbHandler selectAll]);
    
    self.flag = !self.flag;
}

// 点赞跟取消
- (void)goodButtonAction:(UIButton *)button
{
    if (self.flag) {
        self.goodButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"goodEmpty.png"]];
        
        NSInteger upTimes = [self.cellJokes.upTimes integerValue];
        upTimes--;
        self.cellJokes.upTimes = (NSString *)[NSNumber numberWithInteger:upTimes];
        self.goodLabel.text = [NSString stringWithFormat:@":%@", self.cellJokes.upTimes];
    } else {
        
        self.goodButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"goodFull.png"]];
        NSInteger upTimes = [self.cellJokes.upTimes integerValue];
        upTimes++;
        self.cellJokes.upTimes = (NSString *)[NSNumber numberWithInteger:upTimes];
        self.goodLabel.text = [NSString stringWithFormat:@":%@", self.cellJokes.upTimes];
    }
    
    self.flag = !self.flag;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)dealloc
{
    [_cellJokes release];
    [_myView release];
    [_digestLabel release];
    [_collectButton release];
    [_goodButton release];
    [_goodLabel release];
    [_lineLabel release];
    [super dealloc];
}

@end
